#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "operate.h"
#include "myDatatype.h"
#define CAPACITY 100

/*函数原型*/
/*链表操作*/
struct Node *CreateHead()/*头结点初始化*/
{
	struct Node *phead = (struct Node*)malloc(sizeof(struct Node));
	
	phead->data = NULL;
	phead->next = NULL;
	return phead;
}

struct Node *FindTail(struct Node *phead)/*查找尾结点*/
{
	struct Node *p = phead;
	
	while (p->next != NULL)
	{
		p = p->next;
	}
	return p;
}

struct Node *AddList(struct Node *phead, void *(*pInputInfo)())/*尾插法建立链表*/
{
	struct Node *pnew = (struct Node *)malloc(sizeof(struct Node));
	struct Node *ptail = FindTail(phead);
	
	pnew->data = pInputInfo();
	pnew->next = NULL;
	
	ptail->next = pnew;
	return phead;
}

void Release(struct Node *phead)/*释放链表*/
{
	struct Node *p = phead;
	
	while (p != NULL)
	{
		p = p->next;
		free(phead);
		phead = p;
	}
	return;
}

/*数据输入及输出*/
void *InputSup()/*供应商数据输入*/
{
	sup *data = (sup *)malloc(sizeof(sup));
	
	printf("\n\t\t供应商编号：");		
	scanf("%s", data->supNo);
	printf("\t\t供应商名称：");
	scanf("%s", data->supName);
	printf("\t\t供应商简称：");
	scanf("%s", data->supName_short);
	printf("\t\t地址：");
	scanf("%s", data->supAddress);
	printf("\t\t公司电话：");
	scanf("%d", &data->supPhone);
	printf("\t\t邮件：");
	scanf("%s", data->supMail);
	printf("\t\t联系人：");
	scanf("%s", data->supLinkman);
	printf("\t\t联系人电话：");
	scanf("%d", &data->supLinkman_phone);
	printf("\t\t备注：");
	scanf("%s", data->supNote);
	return data;
}

void *InputCom()/*商品数据输入*/
{
	com *data = (com *)malloc(sizeof(com));
	
	printf("\n\t\t商品编号:");
	scanf("%s", data->comNo);
	printf("\t\t商品名称:");
	scanf("%s", data->comName);
	printf("\t\t商品单价:");
	scanf("%lf", &data->comPrice);
	printf("\t\t供应商编号:");
	scanf("%s", data->supNo);
	printf("\t\t商品简介:");
	scanf("%s", data->comIntro);
	printf("\t\t备注:");
	scanf("%s", data->comNote);
	return data;
}

void *InputSta()/*员工数据输入*/
{
	sta *data = (sta *)malloc(sizeof(sta));
	
	printf("\n\t\t员工编号:");
	scanf("%s", data->staNo);
	printf("\t\t员工姓名:");
	scanf("%s", data->staName);
	printf("\t\t员工密码:");
	scanf("%s", data->staPassword);
	printf("\t\t员工级别:");
	scanf("%s", data->staRank);
	printf("\t\t员工电话:");
	scanf("%d", &data->staPhone);
	printf("\t\t员工工资:");
	scanf("%lf", &data->staWage);
	printf("\t\t备注:");
	scanf("%s", data->staNote);
	return data;
}

/*
外码输入：
1、创建指针分别指向主码和外码对应的头结点，指向外码的指针用于存放数据和连接链表，指向主码的指针用于查找数据
2、通过循环向前移动指向主码的指针，与指向外码的指针的值进行比较，并返回int型变量（表示是否相等）
3、若相等则能将存放数据的指针存入链表，反之，提示输入错误并释放存放数据的指针
*/
void *InputMasterPur()/*主表数据输入*/
{
	masterPur *data = (masterPur *)malloc(sizeof(masterPur));
	
	printf("\n\t\t采购清单号:");
	scanf("%s", data->masterPurNo);
	printf("\t\t员工编号:");
	scanf("%s", data->staNo);		
	printf("\t\t采购数量:");
	scanf("%d", &data->purNum);
	printf("\t\t采购总价:");
	scanf("%lf", &data->totalPrice);
	printf("\t\t采购时间:");
	scanf("%s", data->purTime);
	printf("\t\t备注:");
	scanf("%s", data->purNote);
	return data;
}

void *InputDetailPur()/*细表数据输入*/
{
	detailPur *data = (detailPur *)malloc(sizeof(detailPur));
	
	printf("\n\t\t采购明细号:");
	scanf("%s", data->detailPurNo);
	printf("\t\t采购清单号:");
	scanf("%s", data->masterPurNo);		
	printf("\t\t商品编号:");
	scanf("%s", data->comNo);
	printf("\t\t采购数量:");
	scanf("%d", &data->purNum);
	printf("\t\t商品单价:");
	scanf("%lf", &data->singlePrice);
	printf("\t\t商品总价:");
	scanf("%lf", &data->totalPrice);
	printf("\t\t备注:");
	scanf("%s", data->purNote);
	return data;
}

void Traversal(struct Node *phead, void (*pShowInfo)(void *data))/*遍历链表并输出数据*/
{
	struct Node *p = NULL;
	
	p = phead->next;
	
	while (p != NULL)
	{
		pShowInfo(p->data);
		p = p->next;
	}
	return;
}

void ShowSup(void *data)/*供应商数据输出*/
{
	sup *p = (sup *)data;
	
	printf("\n\t\t供应商编号：%s", p->supNo);
	printf("\n\t\t供应商名称：%s", p->supName);
	printf("\n\t\t供应商简称：%s", p->supName_short);
	printf("\n\t\t地址：%s", p->supAddress);
	printf("\n\t\t公司电话：%d", p->supPhone);
	printf("\n\t\t邮件：%s", p->supMail);
	printf("\n\t\t联系人：%s", p->supLinkman);
	printf("\n\t\t联系人电话：%d", p->supLinkman_phone);
	printf("\n\t\t备注：%s\n", p->supNote);
	return;
}

void ShowCom(void *data)/*商品数据输出*/
{
	com *p = (com *)data;
	
	printf("\n\t\t商品编号：%s", p->comNo);
	printf("\n\t\t商品名称：%s", p->comName);
	printf("\n\t\t商品单价：%lf", p->comPrice);
	printf("\n\t\t供应商编号：%s", p->supNo);
	printf("\n\t\t商品简介：%s", p->comIntro);
	printf("\n\t\t备注：%s\n", p->comNote);
	return;
}

void ShowSta(void *data)/*员工数据输出*/
{
	sta *p = (sta *)data;
	
	printf("\n\t\t员工编号: %s", p->staNo);
	printf("\n\t\t员工姓名: %s", p->staName);
	printf("\n\t\t员工密码: %s", p->staPassword);
	printf("\n\t\t员工级别: %s", p->staRank);
	printf("\n\t\t员工电话: %d", p->staPhone);
	printf("\n\t\t员工工资: %lf", p->staWage);
	printf("\n\t\t备注: %s\n", p->staNote);
	return;
}

void ShowMasterPur(void *data)/*采购主表数据输出*/
{
	masterPur *p = (masterPur *)data;
	
	printf("\n\t\t采购清单号: %s", p->masterPurNo);
	printf("\n\t\t员工编号: %s", p->staNo);
	printf("\n\t\t采购数量: %d", p->purNum);
	printf("\n\t\t采购总价: %lf", p->totalPrice);
	printf("\n\t\t采购时间: %s", p->purTime);
	printf("\n\t\t备注: %s\n", p->purNote);
	return;
}

void ShowDetailPur(void *data)/*采购主表数据输出*/
{
	detailPur *p = (detailPur *)data;
	
	printf("\n\t\t采购明细号: %s", p->detailPurNo);
	printf("\n\t\t采购清单号: %s", p->masterPurNo);
	printf("\n\t\t商品编号: %s", p->comNo);
	printf("\n\t\t采购数量: %d", p->purNum);
	printf("\n\t\t商品单价: %lf", p->singlePrice);
	printf("\n\t\t商品总价: %lf", p->totalPrice);
	printf("\n\t\t备注: %s\n", p->purNote);
	return;
}

/*修改信息
1、用string中的strcmp函数比较两个字符串数据是否相等，记录下前一个结点p，由此完成查询操作
2、建立一个新结点pnew(初始化NULL)并输入数据
3、pnew->next = p->next->next;
	p->next = pnew;
*/
void Change(struct Node *phead, struct Node *pnew, char *Var, int (*pCompare)(void *data, char *Var))	/*修改数据*/
{
	struct Node *p = NULL;
	struct Node *q = NULL;
	p = phead;
	q = phead->next;
	
	while (q != NULL)
	{
		if (pCompare(q->data, Var) == 0)
		{
			break;
		}
		p = p->next;
		q = q->next;
	}
	
	pnew->next = p->next->next;
	p->next = pnew;
	
	return;
}

/*删除信息
1、建立一个Node *p；
2、让p指向被删数据的前一结点
3、p->next = p->next->next;
*/
struct Node *Delete(struct Node *phead, char *Var, int (*pCompare)(void *data, char *Var))
{
	struct Node *p = NULL;
	struct Node *q = (struct Node *)malloc(sizeof(struct Node));
	
	p = phead;
	q = phead->next;
	
	while (p != NULL)
	{
		if (pCompare(q->data, Var) == 0)
		{
			break;
		}
		
		p = p->next;
		q = q->next;
	}
	
	p->next = p->next->next;
	return q;
}

/*比较*/
int CompareSupNo(void *data, char *Var)/*比较[供应商编号]与手动输入的字符串*/
{
	sup *p = (sup *)data;
	
	if (strcmp(p->supNo, Var) > 0)
	{
		return 1;
	}
	else if (strcmp(p->supNo, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareSupName(void *data, char *Var)/*比较[供应商名称]与手动输入的字符串*/
{
	sup *p = (sup *)data;
	
	if (strcmp(p->supName, Var) > 0)
	{
		return 1;
	}
	else if (strcmp(p->supName, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareSupAddress(void *data, char *Var)/*比较[地址]与手动输入的字符串*/
{
	sup *p = (sup *)data;
	
	if (strcmp(p->supAddress, Var) > 0)
	{
		return 1;
	}
	else if (strcmp(p->supAddress, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareComNo(void *data, char *Var)/*比较[商品编号]与手动输入的字符串*/
{
	com *p = (com *)data;
	
	if (strcmp(p->comNo, Var) > 0)
	{
		return 1;	
	}	
	else if (strcmp(p->comNo, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareComName(void *data, char *Var)/*比较[商品名称]与手动输入的字符串*/
{
	com *p = (com *)data;
	
	if (strcmp(p->comName, Var) > 0)
	{
		return 1;	
	}	
	else if (strcmp(p->comName, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareStaNo(void *data, char *Var)/*比较[员工编号]与手动输入的字符串*/
{
	sta *p = (sta *)data;
	
	if (strcmp(p->staNo, Var) > 0)
	{
		return 1;	
	}	
	else if (strcmp(p->staNo, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareStaName(void *data, char *Var)/*比较[员工姓名]与手动输入的字符串*/
{
	sta *p = (sta *)data;
	
	if (strcmp(p->staName, Var) > 0)
	{
		return 1;	
	}	
	else if (strcmp(p->staName, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareStaPassword(void *data, char *Var)/*比较[员工密码]与手动输入的字符串*/
{
	sta *p = (sta *)data;
	
	if (strcmp(p->staPassword, Var) > 0)
	{
		return 1;	
	}	
	else if (strcmp(p->staPassword, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareMasterPurNo(void *data, char *Var)/*比较[采购清单号]与手动输入的字符串*/
{
	masterPur *p = (masterPur *)data;
	
	if (strcmp(p->masterPurNo, Var) > 0)
	{
		return 1;	
	}	
	else if (strcmp(p->masterPurNo, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareDetailPurNo(void *data, char *Var)/*比较[采购明细号]与手动输入的字符串*/
{
	detailPur *p = (detailPur *)data;
	
	if (strcmp(p->detailPurNo, Var) > 0)
	{
		return 1;	
	}	
	else if (strcmp(p->detailPurNo, Var) < 0)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int CompareOuterStaNo(void *firstData, void *secondData)	/*比较员工编号和采购主表的员工编号是否一致*/
{
	sta *pSta = (sta *)firstData;
	masterPur *pMasterPur = (masterPur *)secondData;
	
	if (strcmp(pSta->staNo, pMasterPur->staNo) > 0)
	{
		return 1;
	}
	else if (strcmp(pSta->staNo, pMasterPur->staNo) < 0)
	{
		return -1;
	}
	else 
	{
		return 0;
	}
}

int CompareOuterComNo(void *firstData, void *secondData)	/*比较商品编号和采购细表的商品编号是否一致*/
{
	com *pCom = (com *)firstData;
	detailPur *pDetailPur = (detailPur *)secondData;
	
	if (strcmp(pCom->comNo, pDetailPur->comNo) > 0)
	{
		return 1;
	}
	else if (strcmp(pCom->comNo, pDetailPur->comNo) < 0)
	{
		return -1;
	}
	else 
	{
		return 0;
	}
}

int CompareOuterMasterPurNo(void *firstData, void *secondData)	/*比较采购清单号和采购细表的采购清单号是否一致*/
{
	masterPur *pMasterPur = (masterPur *)firstData;
	detailPur *pDetailPur = (detailPur *)secondData;
	
	if (strcmp(pMasterPur->masterPurNo, pDetailPur->masterPurNo) > 0)
	{
		return 1;
	}
	else if (strcmp(pMasterPur->masterPurNo, pDetailPur->masterPurNo) < 0)
	{
		return -1;
	}
	else 
	{
		return 0;
	}
}

struct Node *ReadSup(struct Node *phead, FILE *fp)/*读取供应商信息*/
{
	if(fp == NULL) 
	{
		return phead;
	}
	
	while(1)
	{
		sup *data = (sup *)malloc(sizeof(sup));
		struct Node *p = (struct Node *)malloc(sizeof(struct Node));
		int x = fread(data, sizeof(sup), 1, fp);
		struct Node *q = NULL;
		
		if(x == 0)
		{
			free(p);
			break;
		} 
		
		p->data = data;
		p->next = NULL;	
		/*尾插法*/
		q = FindTail(phead);/*找到尾结点*/
		q->next = p;
	}
	
	fclose(fp);
	return phead;
}

struct Node *ReadCom(struct Node *phead, FILE *fp)/*读取商品信息*/
{
	if(fp == NULL) 
	{
		return phead;
	}
	
	while(1)
	{
		com *data = (com *)malloc(sizeof(com));
		struct Node *p = (struct Node *)malloc(sizeof(struct Node));
		int x = fread(data, sizeof(com), 1, fp);
		struct Node *q = NULL;
		
		if(x == 0)
		{
			free(p);
			break;
		} 
		
		p->data = data;
		p->next = NULL;	
		/*尾插法*/
		q = FindTail(phead);/*找到尾结点*/
		q->next = p;
	}
	
	fclose(fp);
	return phead;
}

struct Node *ReadSta(struct Node *phead, FILE *fp)/*读取员工信息*/
{
	if(fp == NULL) 
	{
		return phead;
	}
	
	while(1)
	{
		sta *data = (sta *)malloc(sizeof(sta));
		struct Node *p = (struct Node *)malloc(sizeof(struct Node));
		int x = fread(data, sizeof(sta), 1, fp);
		struct Node *q = NULL;
		
		if(x == 0)
		{
			free(p);
			break;
		} 
		
		p->data = data;
		p->next = NULL;	
		/*尾插法*/
		q = FindTail(phead);/*找到尾结点*/
		q->next = p;
	}
	
	fclose(fp);
	return phead;
}

struct Node *ReadMaster(struct Node *phead, FILE *fp)/*读取采购主表信息*/
{
	if(fp == NULL) 
	{
		return phead;
	}
	
	while(1)
	{
		masterPur *data = (masterPur *)malloc(sizeof(masterPur));
		struct Node *p = (struct Node *)malloc(sizeof(struct Node));
		int x = fread(data, sizeof(masterPur), 1, fp);
		struct Node *q = NULL;
		
		if(x == 0)
		{
			free(p);
			break;
		} 
		
		p->data = data;
		p->next = NULL;	
		/*尾插法*/
		q = FindTail(phead);/*找到尾结点*/
		q->next = p;
	}
	
	fclose(fp);
	return phead;
}

struct Node *ReadDetail(struct Node *phead, FILE *fp)/*读取采购细表信息*/
{
	if(fp == NULL) 
	{
		return phead;
	}
	
	while(1)
	{
		detailPur *data = (detailPur *)malloc(sizeof(detailPur));
		struct Node *p = (struct Node *)malloc(sizeof(struct Node));
		int x = fread(data, sizeof(detailPur), 1, fp);
		struct Node *q = NULL;
		
		if(x == 0)
		{
			free(p);
			break;
		} 
		
		p->data = data;
		p->next = NULL;	
		/*尾插法*/
		q = FindTail(phead);/*找到尾结点*/
		q->next = p;
	}
	
	fclose(fp);
	return phead;
}

void WriteSup(struct Node* phead, FILE* fp)/*写入供应商信息*/
{
    struct Node *p = phead->next;
        while(p != NULL)
        {
            fwrite(p->data, sizeof(sup), 1, fp);  
            p = p->next;
        }
    fclose(fp);
}

void WriteCom(struct Node* phead, FILE* fp)/*写入商品信息*/
{
    struct Node *p = phead->next;
        while(p != NULL)
        {
            fwrite(p->data, sizeof(com), 1, fp);  
            p = p->next;
        }
    fclose(fp);
}

void WriteSta(struct Node* phead, FILE* fp)/*写入员工信息*/
{
    struct Node *p = phead->next;
        while(p != NULL)
        {
            fwrite(p->data, sizeof(sta), 1, fp);  
            p = p->next;
        }
    fclose(fp);
}

void WriteMaster(struct Node* phead, FILE* fp)/*写入采购主表信息*/
{
    struct Node *p = phead->next;
        while(p != NULL)
        {
            fwrite(p->data, sizeof(masterPur), 1, fp);  
            p = p->next;
        }
    fclose(fp);
}

void WriteDetail(struct Node* phead, FILE* fp)/*写入采购细表信息*/
{
    struct Node *p = phead->next;
        while(p != NULL)
        {
            fwrite(p->data, sizeof(detailPur), 1, fp);  
            p = p->next;
        }
    fclose(fp);
}
